package com.lecampus.businessCustomer.controller;

import com.lecampus.businessCustomer.mapper.UserMapper;
import com.lecampus.commons.entity.User;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author 罗宇航
 */
@RestController
@RequestMapping("/lecampus/user")
public class UserController {

    //注册成功页面
    private final String SUCCESS_REGISTER_PAGE = "";
    //查询成功页面
    private final String SUCCESS_FIND_PAGE = "";

    private String appID = "wx5c15c5822f0b723a";
    private String appSecret = "339b46886458b1a7de45e388f5247944";

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param userName
     * @param realName
     * @param phone
     * @param imageUrl
     */
    @PostMapping("/register")
    public String add(String userName,
                    String realName,
                    String phone,
                    String imageUrl) {

        User user = new User();
        user.setUserName(userName)
                .setRealName(realName)
                .setPhone(phone)
                .setImageUrl(imageUrl);

        userMapper.insert(user);

        return SUCCESS_REGISTER_PAGE;
    }

    /**
     * 登录
     * @param code
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping(value = "/login")
    public String userLogin(@RequestParam("code") String code) throws IOException, ParseException {
        String result="";
        //拼接url
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?");
        url.append("appid=")
                .append(this.appID)
                .append("&secret=")
                .append(this.appSecret)
                .append("&js_code=")
                .append(code)
                .append("&grant_type=authorization_code");
        try (final CloseableHttpClient client = HttpClients.createDefault()){
            final HttpGet get = new HttpGet(url.toString());
            try (final CloseableHttpResponse response = client.execute(get)) {
                final HttpEntity entity = (HttpEntity) response.getEntity();
                result= EntityUtils.toString((org.apache.hc.core5.http.HttpEntity) entity);
                EntityUtils.consume((org.apache.hc.core5.http.HttpEntity) entity);
            }
        }

        return result;
    }


    /**
     * 根据user_id查找并返回用户信息
     * @param userId
     */
    @GetMapping("/findById/{userId}")
    public User select(@PathVariable("userId") String userId) {
        User user = userMapper.selectById(userId);

        return user;
    }
}

